package subject1.designQuestion;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class UndergroundSystem {
    class StartStationNameStartTime {
        String startStationName;
        Integer startTime;
        public StartStationNameStartTime(String startStationName, Integer startTime) {
            this.startStationName = startStationName;
            this.startTime = startTime;
        }
    }

    class StartStationEndStation {
        String startStation;
        String endStation;

        public StartStationEndStation(String startStation, String endStation) {
            this.startStation = startStation;
            this.endStation = endStation;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            StartStationEndStation that = (StartStationEndStation) o;
            return Objects.equals(startStation, that.startStation) && Objects.equals(endStation, that.endStation);
        }

        @Override
        public int hashCode() {
            return Objects.hash(startStation, endStation);
        }
    }

    class SumCount {
        double sum;
        int count;

        public SumCount(double sum, int count) {
            this.sum = sum;
            this.count = count;
        }
    }


    // key -> 乘客ID， value -> 上车站点名称和上车时间
    private Map<Integer, StartStationNameStartTime> map1 = new HashMap<>();
    // key -> 开始站点和结束站点，value -> 总耗时和下车人数
    private Map<StartStationEndStation, SumCount> map2 = new HashMap<>();

    public UndergroundSystem() {

    }

    public void checkIn(int id, String stationName, int t) {
        map1.put(id, new StartStationNameStartTime(stationName, t));
    }

    public void checkOut(int id, String stationName, int t) {
        StartStationNameStartTime startStationNameStartTime = map1.get(id);
        StartStationEndStation startStationEndStation = new StartStationEndStation(startStationNameStartTime.startStationName, stationName);
        SumCount sumCount = map2.getOrDefault(startStationEndStation, new SumCount(0.0, 0));
        sumCount.sum += t - startStationNameStartTime.startTime;
        sumCount.count++;
        map2.put(startStationEndStation, sumCount);
    }

    public double getAverageTime(String startStation, String endStation) {
        SumCount sumCount = map2.get(new StartStationEndStation(startStation, endStation));
        return sumCount.sum / sumCount.count;
    }

    public static void main(String[] args) {
        UndergroundSystem sys = new UndergroundSystem();
        sys.checkIn(1, "aaa", 1);
        sys.checkOut(1, "bbb", 3);
        System.out.println(sys.getAverageTime("aaa", "bbb"));
    }
}

/**
 * Your UndergroundSystem object will be instantiated and called as such:
 * UndergroundSystem obj = new UndergroundSystem();
 * obj.checkIn(id,stationName,t);
 * obj.checkOut(id,stationName,t);
 * double param_3 = obj.getAverageTime(startStation,endStation);
 */